﻿using AutoMapper;
using B.S.BaseData.Domain.Entities;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interfaces;
using B.S.BaseData.Write.API.Command.RoleCommand;
using MediatR;

namespace B.S.BaseData.Write.API.CommandHandler.RoleHandler
{
    public class RoleDelCommandHandler : IRequestHandler<RoleDelCommand, APIResult<object>>
    {
        private readonly IBaseRepository<Role> userrReppos;
        private readonly IMapper mapper;

        public RoleDelCommandHandler(IBaseRepository<Role> userrReppos, IMapper mapper)
        {
            this.userrReppos = userrReppos;
            this.mapper = mapper;
        }


        public Task<APIResult<object>> Handle(RoleDelCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            var obj = userrReppos.GetAll().FirstOrDefault(x => x.RoleId == request.RoleId);
            obj.IsDeleted = true;
            var res = userrReppos.Update(obj);
            if (res == 1)
            {
                result.Code = APIEnum.Success;
                result.Msg = "删除成功";
                result.Data = res;
            }
            else
            {
                result.Code = APIEnum.Failed;
                result.Msg = "删除失败";
                result.Data = res;
            }
            return Task.FromResult(result);
        }
    }
}
